package cn.wolfcode.rabc.service.impl;

import cn.wolfcode.rabc.domain.Department;
import cn.wolfcode.rabc.mapper.DepartmentMapper;
import cn.wolfcode.rabc.qo.QueryObject;
import cn.wolfcode.rabc.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {


    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public Department get(Long id) {
        return departmentMapper.get(id);
    }

    @Override
    @Transactional
    public List<Department> listAll() {
        return departmentMapper.listAll();
    }

    /*@Override
    @Transactional
    public PageResult<Department> query(QueryObject qo) {
        int count = departmentMapper.selectForCount(qo);
        if(count==0){
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(), Collections.emptyList(),count);
        }

        List<Department> PageResult = departmentMapper.selectForList(qo);

        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),PageResult,count);
    }*/

    public PageInfo<Department> query(QueryObject qo){
        // 使用分插件，当传入前页，每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Department> departments = departmentMapper.selectForList(qo);
        return new PageInfo(departments);
    }
}
